define(function (require, exports, module) {

  require("mixins/global.mixins.js");
  var mCard = require('andrew/card/index.js');
  var toastr = require("third/toastr/index.js");

  var api = require('service/api.js');

  var pageVue = new Vue({
    data: {
      tableList: [],
      searchForm: {
        id: null,
        name: null,
        parentCategoryId: null,
        categoryId: null,
        dataFlag: 1
      },
      dataFlagEnum: [],
      OneCategoryList: [],
      TwoCategoryList: [],
      selectedOptions: [],
      formCollapse: false,
      pageSizes: [5, 10, 20, 30],
      currentPage: 1,
      pageSize: 5,
      totalCount: 0
    },
    mounted: function () {
      this.getOneCategoryList();
      this.getDataList();

      console.log(this.Enum);
    },
    methods: {
      getOneCategoryList: function () {
        var curPageVue = this;

        api.queryCategoryList({
          params: {
            pId: 0
          },
          callBack: function (res) {
            curPageVue.OneCategoryList = res;
            console.log('SimpleCategoryList');
            console.log(res);

          }
        })
      },
      getChildrenCategoryList: function (id) {
        var curPageVue = this;

        api.queryCategoryList({
          params: {
            pId: id
          },
          callBack: function (res) {
            curPageVue.TwoCategoryList = res;
            console.log('SimpleCategoryList');
            console.log(res);

          }
        })
      },
      categoryChange: function (val) {
        this.searchForm.categoryId = null;
        this.TwoCategoryList = [];
        this.getChildrenCategoryList(val);
      },
      handleChange: function (value) {
        console.log(value);
      },
      clearSearch: function () {
        this.searchForm.id = null;
        this.searchForm.name = null;
        this.searchForm.dataFlag = null;
        this.searchForm.categoryId = null;
        this.searchForm.parentCategoryId = null;
        this.selectedOptions = [null];
      },
      clearThenSearch: function () {
        this.clearSearch();
        this.getDataList();
      },
      toggleSearhBodyCollapse: function () {
        this.formCollapse = !this.formCollapse;
      },
      onSubmit: function () {
        this.getDataList();
      },
      getDataList: function () {

        var curPageVue = this;

        var query = {};

        if (curPageVue.searchForm.id != null && curPageVue.searchForm.id.length > 0) {
          query['bookId'] = curPageVue.searchForm.id;
        }
        if (curPageVue.searchForm.name && curPageVue.searchForm.name.length > 0) {
          query['bookName'] = curPageVue.searchForm.name;
        }
        if (curPageVue.searchForm.dataFlag != null && curPageVue.searchForm.dataFlag >= 0) {
          query['dataFlag'] = curPageVue.searchForm.dataFlag;
        }
        if (curPageVue.searchForm.categoryId != null && curPageVue.searchForm.categoryId >= 0) {
          query['categoryId'] = curPageVue.searchForm.categoryId;
        }
        if (curPageVue.searchForm.parentCategoryId != null && curPageVue.searchForm.parentCategoryId >= 0) {
            query['parentCategoryId'] = curPageVue.searchForm.parentCategoryId;
        }

        query['pageSize'] = curPageVue.pageSize;
        query['pageNo'] = curPageVue.currentPage;

        api.getArtList({
          params: query,
          callBack: function (res) {
            curPageVue.tableList = res.list;
            curPageVue.totalCount = res.total;
          }
        })
      },
      handleEdit: function (bookId) {
        pageVue = this;

        // iframe窗
        var index = layer.open({
          type: 2,
          title: '文章基本信息展示',
          shade: [0],
          area: ['50%', '70%'],
          content: ['04_artInfo.html?bookId=' + bookId], // iframe的url，no代表不显示滚动条
          end: function () { // 此处用于演示
            pageVue.getDataList()
          }
        })
      },
      handleDelete: function (index, row) {
        console.log(index, row)
      },
      handleCommand: function (command, row) {
        var bookId = row.bookId;
        pageVue = this;
        if (command === "chapter") {
          window.location.href = "06_chapterList.html?bookId=" + bookId;
        } else if (command === "delete") {
            api.changeArtByBookId({
                params: {
                  bookId: bookId,
                  dataFlag: row.dataFlag
                },
                callBack: function (res) {
                  if(res > 0){
                      toastr.error("操作成功");
                      pageVue.getDataList();
                  }
                }
            })
        } else if (command === "info") {
          this.handleEdit(bookId);
        }
      },
      handleSizeChange: function (val) {
        this.pageSize = val;
        this.currentPage = 1;
        this.getDataList();
      },
      handleCurrentChange: function (val) {
        this.currentPage = val;
        this.getDataList();
      }

    }

  }).$mount('#app')
})
